package com.chinasoft.obj;

import com.chinasoft.enumdef.Direction;
import com.chinasoft.enumdef.Elem;
import com.chinasoft.enumdef.Way;
import lombok.Data;

/**
 *
 */
@Data
public class Man implements CommonAble {
    int manX;
    int manY;
    Elem[][] map;
    MapData data;
    // 人的位置以前是什么
    Elem manBk = Elem.Glass;
    Direction manDir;

    public void dealMove(Direction dr) {
        // 判断行动的性质
        Way way = pushOrMove(dr);
        System.out.println("way is " + way);
//        System.out.println("move dr is " + dr);
        if (way == Way.Move) {
            if (canMove(dr)) {
                // alt+enter
                move(dr);
            }
        } else {
            if (data.isPushGroup(dr)) {
                System.out.println("box group move");
                BoxGroup group = data.getGroup();
                group.init(this, dr);
                if (group.canMove(dr)) {
                    group.move(dr);
                    move(dr);
                }
            } else {
                // 就是 push 方式
                if (canPush(dr)) {
                    realPush(dr);
                }
            }
        }

    }

    private void realPush(Direction dr) {
        // 获取箱子对象
        Box box = this.getNextBox(dr);
        // 让箱子移动
        box.move(dr);
        // 让人移动
        move(dr);
    }

    private boolean canPush(Direction dr) {
        // 获取箱子后面的对象
        Box nextBox = this.getNextBox(dr);
        // 如果是 草地 或者 是 金子， 可以推
        return nextBox.canMove(dr);
    }

    /**
     * 判断行动的性质， 是 移动还是 推箱子
     *
     * @param dr： 人移动的方向
     * @return Move: 移动
     * Push： 推箱子
     */
    private Way pushOrMove(Direction dr) {
        Elem obj = getNextObj(dr);
        // 3 是箱子  9 是带金子的箱子
        if (obj == Elem.Box || obj == Elem.BoxGold) {
            return Way.Push;
        } else {
            return Way.Move;
        }
    }

    @Override
    public void move(Direction way) {
        switch (way) {
            case Up:
                moveUp();
                break;
            case Down:
                moveDown();
                break;
            case Left:
                moveLeft();
                break;
            case Right:
                moveRight();
                break;
        }
    }

    @Override
    public Elem getNextObj(Direction dr) {
        return data.getNextCode(this, dr);
    }

    @Override
    public boolean canMove(Direction way) {
        System.out.println("way is " + way);
        if (this.getNextObj(way) == Elem.House) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public Box getNextBox(Direction dr) {
        return data.getNextBox(this, dr);
    }


    public void moveRight() {
        if (manX < 19) {
            // 恢复当前位置
            map[manY][manX] = manBk;
            // 坐标移动
            manX++;
            // 备份目标位置
            manBk = map[manY][manX];
            // 人移动
            map[manY][manX] = Elem.Man;
            this.manDir = Direction.Right;
        }
        System.out.println("man X is " + manY);
    }

    public void moveUp() {
        if (manY > 0) {
            map[manY][manX] = manBk;
            manY--;
            manBk = map[manY][manX];
            map[manY][manX] = Elem.Man;
            this.manDir = Direction.Up;
        }
    }

    public void moveDown() {
        if (manY < 19) {
            map[manY][manX] = manBk;
            manY++;
            manBk = map[manY][manX];
            map[manY][manX] = Elem.Man;
            this.manDir = Direction.Down;
        }
        System.out.println("man Y is " + manY);
    }

    public void moveLeft() {
        if (manX > 0) {
            map[manY][manX] = manBk;
            manX--;
            manBk = map[manY][manX];
            map[manY][manX] = Elem.Man;
            this.manDir = Direction.Left;
        }
    }
}
